package constant.milk.periodapp.memo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import constant.milk.periodapp.R;
import constant.milk.periodapp.SuperActivity;
import constant.milk.periodapp.bean.MemoData;
import constant.milk.periodapp.database.DBSelectQuery;
import constant.milk.periodapp.database.DbDeleteQuery;
import constant.milk.periodapp.dialog.MilkDayDialog;
import constant.milk.periodapp.dialog.MilkMonthDialog;
import constant.milk.periodapp.dialog.MilkMonthDialog.DateCallBank;
import constant.milk.periodapp.dialog.MilkYesNoDialog;
import constant.milk.periodapp.util.MilkUtil;

public class MemoListActivity extends SuperActivity
{
	public static final String MEMO_LIST_FLAG = "MEMO_LIST_FLAG";
	public static final String MEMO_YEAR = "MEMO_YEAR";
	public static final String MEMO_MONTH = "MEMO_MONTH";
	public static final String MEMO_DAY = "MEMO_DAY";
	
	public static final int ALL = 0;
	public static final int DATE = 1;
	
	private static final int REQUEST_CODE = 0;

	private TextView backTextView;
	
	private TextView insertTextView;
	private TextView insert2TextView;
	
	private TextView preTextView;
	private TextView todayTextView;
	private TextView nextTextView;
	
	private ListView listView;
	
	private DBSelectQuery select;
	
	private int flag;
	
	private Calendar cal;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.memo_list_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		backTextView = (TextView) findViewById(R.id.memoBackTextView);
		insertTextView = (TextView) findViewById(R.id.memoInsertTextView);
		insert2TextView = (TextView) findViewById(R.id.memoInsert2TextView);
		
		preTextView = (TextView) findViewById(R.id.memoPreTextView);
		todayTextView = (TextView) findViewById(R.id.memoTodayTextView);
		nextTextView = (TextView) findViewById(R.id.memoNextTextView);
		
		listView = (ListView) findViewById(R.id.memoListView);
	}

	@Override
	protected void setOnClick()
	{
		backTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		
		insertTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MemoInfoActivity.class);
				intent.putExtra(MemoInfoActivity.MEMO_FLAG, MemoInfoActivity.INSERT);
				intent.putExtra(MEMO_LIST_FLAG, flag);
				intent.putExtra(MEMO_YEAR, cal.get(Calendar.YEAR));
				intent.putExtra(MEMO_MONTH, cal.get(Calendar.MONTH));
				intent.putExtra(MEMO_DAY, cal.get(Calendar.DAY_OF_MONTH));
				startActivityForResult(intent, REQUEST_CODE);
			}
		});
		
		insert2TextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MemoInfoActivity.class);
				intent.putExtra(MemoInfoActivity.MEMO_FLAG, MemoInfoActivity.INSERT);
				intent.putExtra(MEMO_LIST_FLAG, flag);
				intent.putExtra(MEMO_YEAR, cal.get(Calendar.YEAR));
				intent.putExtra(MEMO_MONTH, cal.get(Calendar.MONTH));
				intent.putExtra(MEMO_DAY, cal.get(Calendar.DAY_OF_MONTH));
				startActivityForResult(intent, REQUEST_CODE);
			}
		});
		
		preTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (flag == ALL)
				{
					cal.add(Calendar.MONTH, -1);
				}
				else
				{
					cal.add(Calendar.DAY_OF_MONTH, -1);
				}
				
				setMemoData();
			}
		});
		
		todayTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (flag == ALL)
				{
					MilkMonthDialog dialog = new MilkMonthDialog(superContext, new DateCallBank()
					{
						@Override
						public void getDate(int year, int month)
						{
							cal.set(year, month, cal.get(Calendar.DAY_OF_MONTH));
							
							setMemoData();
						}
					}, cal.get(Calendar.YEAR));
					
					dialog.show();
				}
				else
				{
					MilkDayDialog dialog = new MilkDayDialog(superContext, new MilkDayDialog.DateCallBank()
					{
						@Override
						public void getDate(int year, int month, int day)
						{
							cal.set(year, month, day);
							
							setMemoData();
						}
					}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
					
					dialog.show();
				}
			}
		});
		
		nextTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (flag == ALL)
				{
					cal.add(Calendar.MONTH, 1);
				}
				else
				{
					cal.add(Calendar.DAY_OF_MONTH, 1);
				}
				
				setMemoData();
			}
		});
	}

	@Override
	protected void setData()
	{
		select = new DBSelectQuery(superContext, superDb);
		
		cal = Calendar.getInstance();
		
		Intent intent = getIntent();
		
		flag = intent.getIntExtra(MEMO_LIST_FLAG, 0);
		
		if (flag == ALL)
		{
			int year = intent.getIntExtra(MEMO_YEAR, cal.get(Calendar.YEAR));
			int month = intent.getIntExtra(MEMO_MONTH, cal.get(Calendar.MONTH));
			int day = intent.getIntExtra(MEMO_DAY, cal.get(Calendar.DAY_OF_MONTH));
			
			cal.set(year, month, day);
		}
		else
		{
			int year = intent.getIntExtra(MEMO_YEAR, cal.get(Calendar.YEAR));
			int month = intent.getIntExtra(MEMO_MONTH, cal.get(Calendar.MONTH));
			int day = intent.getIntExtra(MEMO_DAY, cal.get(Calendar.DAY_OF_MONTH));
			
			cal.set(year, month, day);
		}
		
		setMemoData();
	}

	private void setMemoData()
	{
		ArrayList<MemoData> list = new ArrayList<MemoData>();
		
		if (flag == ALL)
		{
			list = select.getMemoListMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH));
			todayTextView.setText(MilkUtil.getDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1));
		}
		else
		{
			list = select.getMemoListDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
			todayTextView.setText(MilkUtil.getDateCom(
					cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH)));
		}
		
		listView.setOnItemClickListener(oicl);
		
		MemoAdapter adapter = new MemoAdapter(superContext, R.layout.list_item_memo, list);
		listView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}
	
	private OnItemClickListener oicl = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			MemoData data = (MemoData) parent.getItemAtPosition(position);
			
			Intent intent = new Intent(superContext, MemoInfoActivity.class);
			intent.putExtra(MemoInfoActivity.MEMO_FLAG, MemoInfoActivity.INFO);
			intent.putExtra(MemoInfoActivity.MEMO_ID, data.getId());
			startActivityForResult(intent, REQUEST_CODE);
		}
	};
	
	private boolean setDeleteData(int id)
	{
		DbDeleteQuery query = null;
		
		try
		{
			query = new DbDeleteQuery(superContext, superDb);
			
			query.setMemoDelete(id);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == REQUEST_CODE)
		{
			setMemoData();
			setResult(Activity.RESULT_OK);
		}
	}
	
	private MilkYesNoDialog yesNoDialog;
	
	private class MemoAdapter extends ArrayAdapter<MemoData>
	{
		public MemoAdapter(Context context, int resource, List<MemoData> objects)
		{
			super(context, resource, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null)
			{
				LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = li.inflate(R.layout.list_item_memo, null);
			}
			
			final MemoData data = getItem(position);
			
			TextView dateTextView = (TextView) v.findViewById(R.id.listItemDateTextView);
			TextView updateTextView = (TextView) v.findViewById(R.id.listItemUpdateTextView);
			TextView deleteTextView = (TextView) v.findViewById(R.id.listItemDeleteTextView);
			TextView memoTextView = (TextView) v.findViewById(R.id.listItemMemoTextView);
			
			Calendar cal = Calendar.getInstance();
			cal.set(data.getYear(), data.getMonth(), data.getDay());
			
			dateTextView.setText(MilkUtil.getDateCom(data.getYear(), data.getMonth()+1, data.getDay()) + 
					" " + MilkUtil.getWeekDay(cal.get(Calendar.DAY_OF_WEEK)));
			
			memoTextView.setText(data.getMemo());
			
			updateTextView.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					Intent intent = new Intent(superContext, MemoInfoActivity.class);
					intent.putExtra(MemoInfoActivity.MEMO_FLAG, MemoInfoActivity.UPDATE);
					intent.putExtra(MemoInfoActivity.MEMO_ID, data.getId());
					startActivityForResult(intent, REQUEST_CODE);
				}
			});
			
			deleteTextView.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					yesNoDialog = new MilkYesNoDialog(superContext, "확인", "메모를 삭제 하시겠습니까?", 
							new OnClickListener()
					{
						@Override
						public void onClick(View v)
						{
							setDeleteData(data.getId());
							
							setData();
							
							setResult(Activity.RESULT_OK);
							
							yesNoDialog.dismiss();
						}
					});
					
					yesNoDialog.show();
				}
			});
			
			return v;
		}
	}
}
